package com.example.wuqiu.module.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.wuqiu.common.QueryField;
import com.example.wuqiu.module.sys.entity.SysDictData;
import com.example.wuqiu.module.sys.dao.SysDictDataMapper;
import com.example.wuqiu.module.sys.entity.dto.VersionDto;
import com.example.wuqiu.module.sys.service.SysDictDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 字典数据表 服务实现类
 * </p>
 *
 * @author RedStar
 * @since 2022-11-10
 */
@Service
public class SysDictDataServiceImpl extends ServiceImpl<SysDictDataMapper, SysDictData> implements SysDictDataService {
    @Resource
    SysDictDataMapper sysDictDataMapper;
    /*
    * 翻译key值
    * dictId
    * key
    * */
    public String getValueById(String dictId, String key){
        SysDictData sysDictData = new SysDictData();
        sysDictData.setDictId(dictId);

        SysDictData one = this.getOne(createQueryWrapper(sysDictData));
        if (one != null && StringUtils.isNotBlank(one.getDictValue())){
            return one.getDictValue();
        }

        return "";
    }
    public String getValue(String dictKey, String key){
        SysDictData sysDictData = new SysDictData();
        sysDictData.setDictKey(dictKey);

        SysDictData one = this.getOne(createQueryWrapper(sysDictData));
        if (one != null && StringUtils.isNotBlank(one.getDictValue())){
            return one.getDictValue();
        }

        return "";
    }

    @Override
    public LambdaQueryWrapper<SysDictData> createQueryWrapper(SysDictData param){
        LambdaQueryWrapper<SysDictData> queryWrapper = new LambdaQueryWrapper<>();

        if (StringUtils.isNotBlank(param.getDictKey())){
            queryWrapper.eq(SysDictData::getDictKey, param.getDictKey());
        }
        if (StringUtils.isNotBlank(param.getDictId())){
            queryWrapper.eq(SysDictData::getDictId, param.getDictId());
        }
        if (StringUtils.isNotBlank(param.getDictValue())){
            queryWrapper.eq(SysDictData::getDictValue, param.getDictValue());
        }

        if (StringUtils.isNotBlank(param.getStatus())) {
            if(!"all".equals(param.getStatus())) {
                queryWrapper.eq(SysDictData::getStatus, param.getStatus());
            }
        } else {
//            queryWrapper.eq(SysDictData::getStatus, QueryField.STATU_NOR);
        }
        if (StringUtils.isNotBlank(param.getOrderBy())) {
            if (StringUtils.isNotBlank(param.getOrderByType())
                    && QueryField.ASC.equals(param.getOrderByType())) {
                queryWrapper.orderByAsc(SysDictData::getStatus);
            } else {
                queryWrapper.orderByDesc(SysDictData::getOrderBy);
            }
        } else {
            queryWrapper.orderByAsc(SysDictData::getTreeSort);
        }
        return queryWrapper;
    }
    /**
     * @param dictType 字典code
     * @param status 状态：0正常 1删除 2停用
     * */
    public List<SysDictData> getDataByType(String dictType, String status){
        List<SysDictData> sysDictDataList = sysDictDataMapper.getDataByType(dictType, status);

        return sysDictDataList;
    }
    /**
     * @param dictType 字典code
     * @param status 状态：0正常 1删除 2停用
     * */
    @Override
    public SysDictData getDataByTypeOne(String dictType, String status){
        SysDictData sysDictData = sysDictDataMapper.getDataByTypeOne(dictType, status);
        return sysDictData;
    }

    @Override
    public VersionDto getVersion() {
        SysDictData appVersion = getDataByTypeOne("appVersion", "1");

        VersionDto versionDto = new VersionDto();
        versionDto.setDescribe(appVersion.getRemarks());
        versionDto.setEdition_number(appVersion.getDictValue());
        versionDto.setEdition_name(appVersion.getDictValue());
        versionDto.setEdition_url(appVersion.getDictKey());
        versionDto.setEdition_force("1");
        versionDto.setPackage_type("0");
        versionDto.setEdition_issue("1");
        versionDto.setEdition_silence("0");

        return versionDto;
    }
}
